/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package py.una.pol.ii.is2.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import py.una.pol.ii.is2.dao.registro.ChoferDAO;
import py.una.pol.ii.is2.domain.registro.Chofer;

/**
 *
 * @author markos
 */
public class AutocompleteChofer extends HttpServlet {

    @EJB
    private ChoferDAO choferDao;
    private List<Chofer> listaChofer;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        System.out.println("Hello markos =)");
        try {
            String nombreApellido = request.getParameter("txtChofer");
            String condicionChofer = " where upper(c.chofNombre) like '" +
                    nombreApellido.toUpperCase() + "%' or upper(c.chofApellido) " +
                    "like '" + nombreApellido.toUpperCase() + "%' ";
            listaChofer = choferDao.getListaChoferFiltro(condicionChofer);
            out.println("<ul>");
            for (Chofer chofer : listaChofer) {
                out.println("<li id=\"" + chofer.getChofCod().toString() + "\">" + chofer.getChofNombre() + " " + chofer.getChofApellido() + "</li>");
            }
            out.println("</ul>");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
